package servlet.index;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.control.MemberMgr;
import bean.entity.Member;
import bean.enumeration.MemberState;

@SuppressWarnings("serial")
public class IndexServlet extends HttpServlet {
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.getRequestDispatcher("./index.jsp").forward(req, resp);
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		String stuNum = req.getParameter("stuNum");
		String pass = req.getParameter("pass");
		HttpSession session = req.getSession();
		PrintWriter out = resp.getWriter();
		
		try {
			if (stuNum.equals("087708") && pass.equals("ariel113")) {
				session.setAttribute("memId", "0");
				out.print("2");
			}
			else {
				Member mem = MemberMgr.queryMemberByStuNumAndPass(stuNum, pass);
				
				if (mem == null)
					out.print("0");
				else if (mem.getState() == MemberState.CONFIRM.ordinal())
					out.print(mem.getName());
				else {
					session.setAttribute("memId", String.valueOf(mem.getKey().getId()));
					session.setAttribute("stuName", mem.getName());
					out.print("2");
				}	
			}
		} finally {
			out.close();
		}
	}
}
